import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    isLogin: false,
    uname: "",
    trust: false,
  },
  mutations: {
    refresh(state) {
      if (localStorage.getItem("isLogin")) {
        state.isLogin = localStorage.getItem("isLogin");
        state.uname = localStorage.getItem("uname");
        state.trust = localStorage.getItem("trust");
      } else if (sessionStorage.getItem("isLogin")) {
        state.isLogin = sessionStorage.getItem("isLogin");
        state.uname = sessionStorage.getItem("uname");
        state.trust = sessionStorage.getItem("trust");
      } else {
        state.isLogin = 0;
        state.uname = "";
        state.trust = "";
      }
    },
    login(state, payload) {
      let trust = confirm(
        "如果您是当前计算机的主人，请点击确认，点击取消当前登录信息在您关闭此页面后将清除"
      );
      state.isLogin = true;
      state.uname = payload.uname;
      if (trust == true) {
        state.trust = true;
        localStorage.setItem("isLogin", 1);
        localStorage.setItem("uname", payload.uname);
        localStorage.setItem("upwd", payload.upwd);
      } else {
        state.trust = false;
        sessionStorage.setItem("isLogin", 1);
        sessionStorage.setItem("uname", payload.uname);
        sessionStorage.setItem("upwd", payload.upwd);
      }
    },
    unlogin(state) {
      localStorage.setItem("isLogin", 0);
      localStorage.removeItem("uname");
      localStorage.removeItem("upwd");
      sessionStorage.setItem("isLogin", 0);
      sessionStorage.removeItem("uname");
      sessionStorage.removeItem("upwd");
      state.isLogin = false;
    },
  },
  actions: {},
  modules: {},
});
